package com.company.project.controller;
import com.company.project.core.Result;
import com.company.project.core.ResultGenerator;
import com.company.project.entity.ResultEntity;
import com.company.project.service.ResultService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;
import tk.mybatis.mapper.entity.Condition;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;

/**
* Created by CodeGenerator on 2024/06/19.
*/
@RestController
@RequestMapping("/result")
public class ResultController {
    @Resource
    private ResultService resultService;

    @PostMapping("/add")
    public Result add(@RequestBody ResultEntity resultEntity) {
        resultService.save(resultEntity);
        return ResultGenerator.genSuccessResult();
    }

    @GetMapping("/delete")
    public Result delete(@RequestParam Integer id) {
        resultService.deleteById(id);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/update")
    public Result update(@RequestBody ResultEntity resultEntity) {
        resultService.update(resultEntity);
        return ResultGenerator.genSuccessResult();
    }

    @GetMapping("/detail")
    public Result detail(@RequestParam Integer id) {
        ResultEntity resultEntity = resultService.findById(id);
        return ResultGenerator.genSuccessResult(resultEntity);
    }

    @GetMapping("/list")
    public Result list(@RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "0") Integer size,String name) {
        PageHelper.startPage(page, size);
        Condition condition = new Condition(ResultEntity.class);
        Example.Criteria criteria = condition.createCriteria();
        if (StringUtils.isNotEmpty(name)){
            criteria.andLike("name","%"+name+"%");
        }
        List<ResultEntity> list = resultService.findByCondition(condition);
        PageInfo pageInfo = new PageInfo(list);
        return ResultGenerator.genSuccessResult(pageInfo);
    }
}
